<?php
namespace App\Services;

use App\Inputs\UsersInputs;
use App\Models\TouristVisit;
use App\Models\Users;
use App\Models\UserLoginLog;
use phpDocumentor\Reflection\DocBlock\Tags\See;
use phpDocumentor\Reflection\Types\Self_;

class UsersServices extends BaseService {


    /**
     * 根据用户名获取用户
     * @param $username
     * @return User|null|Model
     */
    public function getByUsresname($username){
        return Users::query()->where('username',$username)->where('deleted_at',null)->first();
    }
    /**
     * 根据手机号获取用户
     * @param $phone
     * @return User|null|Model
     */
    public function getByPhone($phone){
        return Users::query()->where('phone',$phone)->where('deleted_at',null)->first();
    }

    /**
     * 根据邮箱获取用户
     * @param $email
     * @return User|null|Model
     */
    public function getByEmail($email){
        return Users::query()->where('email',$email)->where('deleted_at',null)->first();
    }
    /**
     * 获取用户列表
     * @return
     *
     */
    public function listUsers($role_id,$username,$name,$school_id,$type,$page,$limit,$sort,$order) {
        $query = Users::leftJoin('schools', 'schools.id', '=', 'users.school_id')
                        ->leftJoin('roles', 'roles.id', '=', 'users.role_id')
                        ->select('users.id','users.username','users.name','users.type','users.name','users.pic','users.sex','users.age','users.email',
                            'users.phone','users.created_at','users.role_id','users.school_id',
                            'schools.name as school_name','roles.name as role_name');

        if(!empty($username)){
            $query->where('username','like',"%$username%");
        }
        if(!empty($name)){
            $query->where('users.name','like',"%$name%");
        }
        if(isset($school_id)){
            $query->where('school_id',$school_id);
        }
        if(isset($role_id)){
            $query->where('role_id',$role_id);
        }
        if(isset($type)){
            $query->where('type',$type);
        }

        return $query->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);
    }
    /**
     * 返回密码
     * @return
     */
    public function getAuthPassword() {

    }
    /**
     * 获取游客访问日志列表
     * @return
     *
     */
    public function touristList($page,$limit,$sort,$order) {
        $query = TouristVisit::query();
        return $query->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);
    }
    /**
     * 获取用户登录日志列表
     * @return
     *
     */
    public function userLoginLogList($username,$name,$type,$page,$limit,$sort,$order) {
        $query = UserLoginLog::query();
        if(!empty($username)){
            $query->where('username','like',"%$username%");
        }
        if(!empty($name)){
            $query->where('name','like',"%$name%");
        }
        if(!empty($type)){
            $query->where('type','like',"%$type%");
        }
        return $query->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);
    }
    /**
     * 获取用户权限
     * @return array
     */
    public function usersPermissions($user_id) {
        $query = Users::leftJoin('role_users', 'schools.id', '=', 'users.school_id')
            ->leftJoin('roles', 'roles.id', '=', 'users.role_id')
            ->select('users.*','schools.name as school_name','roles.name as role_name');

        if(!empty($username)){
            $query->where('username','like',"%$username%");
        }
        if(!empty($school_id)){
            $query->where('school_id',$school_id);
        }
        if(!empty($type)){
            $query->where('type',$type);
        }

        return $query->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);
    }
}
